<template>
<div>
  <!-- 卡片区域 -->
   <el-card class="box-card">
  <div slot="header" class="clearfix cardheader" >
    <span>文章分类</span>
    <el-button size="mini" type="primary" @click="addVisible=true">添加分类</el-button>
  </div>
  <!-- 表格区域 -->
  <el-table :data="cateList" border stripe>
    <el-table-column type="index" label="序号"></el-table-column>
    <el-table-column label="分类名称" prop="cate_name"></el-table-column>
    <el-table-column label="分类别名" prop="cate_alias"></el-table-column>
    <el-table-column label="操作" >
      <template v-slot="{ row }">
      <el-button type="primary" size="mini" @click="showUpdate(row)">修改</el-button>
      <el-button type="danger" size="mini" @click="delAddCate(row.id)">删除</el-button>
      </template>
    </el-table-column>
  </el-table>

</el-card>
<!-- 提示框区域 -->
<el-dialog
  title="添加分类"
  :visible.sync="addVisible"
  width="35%"
  :close-on-click-modal="false"
  @closed="onAddClosed"
  >
  <!-- 表单区域 -->
  <el-form :model="addForm" label-width="80px" :rules="formRules" ref="addFormRef">
    <el-form-item prop="cate_name" label="分类名称">
      <el-input v-model="addForm.cate_name"></el-input>
    </el-form-item>
    <el-form-item prop="cate_alias" label="分类名称">
      <el-input v-model="addForm.cate_alias"></el-input>
    </el-form-item>
  </el-form>
  <span slot="footer" class="dialog-footer">
    <el-button @click="addVisible = false" size="mini">取 消</el-button>
    <el-button type="primary" @click="addNewCate" size="mini">确 定</el-button>
  </span>
</el-dialog>
<!-- 修改对话框 -->
<el-dialog
  title="修改分类"
  :visible.sync="editVisiable"
  width="30%"
  >
  <el-form :model="editForm" label-width="80px" :rules="formRules" ref="editForm">
     <el-form-item prop="cate_name" label="分类名称">
      <el-input v-model="editForm.cate_name"></el-input>
    </el-form-item>
    <el-form-item prop="cate_alias" label="分类名称">
      <el-input v-model="editForm.cate_alias"></el-input>
    </el-form-item>
  </el-form>
  <span slot="footer" class="dialog-footer">
    <el-button @click="editVisiable = false">取 消</el-button>
    <el-button type="primary" @click="updateCate">修改</el-button>
  </span>
</el-dialog>
</div>
</template>

<script>
import { getCateListAPI, addCateAPI, editCateAPI, delCateAPI } from '@/api/arcitle.js'
export default {
  name: 'Arcitle',
  data() {
    return {
      cateList: [],
      addVisible: false,
      editVisiable: false,
      editForm: {
        id: '',
        cate_name: '',
        cate_alias: ''
      },
      addForm: {
        cate_name: '',
        cate_alias: ''
      },
      formRules: {
        cate_name: [
          { required: true, message: '名字必填', trigger: 'blur' },
          { pattern: /^\S{1,10}$/, message: '名称是1-10位非空字符', trigger: 'blur' }
        ],
        cate_alias: [
          { required: true, message: '名字必填', trigger: 'blur' },
          { pattern: /^[a-zA-Z0-9]{1,15}$/, message: '别名是1-15位字母数字', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.initCateList()
  },
  methods: {
    async initCateList() {
      const { data: res } = await getCateListAPI()
      if (res.code !== 0) {
        this.$message.error('获取文章分类列表失败')
      } else {
        // console.log(res.data)
        this.cateList = res.data
      }
    },
    onAddClosed() {
      this.$refs.addFormRef.resetFields()
    },
    addNewCate() {
      this.$refs.addFormRef.validate(async valid => {
        if (!valid) return
        const { data: res } = await addCateAPI(this.addForm)
        if (res.code !== 0) return this.$message.error('添加分类失败')
        this.$message.success('添加分类成功')
        this.addVisible = false
        this.initCateList()
      })
    },
    async delAddCate(id) {
      if (id === 1 || id === 2) return this.$message.error('删除文章分类失败')
      const result = await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (result === 'cancel') return
      const { data: res } = await delCateAPI(id)
      if (res.code !== 0) return this.$message.error('删除文章分类失败')
      this.$message.success('删除文章分类成功')
      this.initCateList()
    },
    showUpdate(row) {
      if (row.id === 1 || row.id === 2) return this.$message.error('前两条数据不能修改')
      // 浅拷贝，防止数据互相影响
      this.editForm = Object.assign({}, row)
      this.editVisiable = true
    },
    updateCate() {
      this.$refs.editForm.validate(async(valid) => {
        if (!valid) return
        const { data: res } = await editCateAPI(this.editForm)
        if (res.code !== 0) return this.$message.error('更新分类失败')
        this.$message.success('修改分类成功')
        this.editVisiable = false
        this.initCateList()
      })
    }
  }
}
</script>

<style lang="less" scoped>
  .cardheader {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
</style>
